{
  LX IO module

  Based on modo's SDK lxio.h which is:

  Copyright (c) 2008-2012 Luxology LLC

  Permission is hereby granted, free of charge, to any person obtaining a
  copy of this software and associated documentation files (the 'Software'),
  to deal in the Software without restriction, including without limitation
  the rights to use, copy, modify, merge, publish, distribute, sublicense,
  and/or sell copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.   Except as contained
  in this notice, the name(s) of the above copyright holders shall not be
  used in advertising or otherwise to promote the sale, use or other dealings
  in this Software without prior written authorization.

  THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER  AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM,  OF OR  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  DEALINGS  THE SOFTWARE.
}

{
  Copyright (c) 2012 David Ballesteros and Jameel Halabi

  This unit is part of the BLAISEmodo wrapper,
  which is licensed under the BSD 2-Clause license (see licenses/license.txt).
}


unit Lx_IO;

{$mode objfpc}{$H+}

interface

uses
  Lx_Result, Lx_Server;

const
  LXa_LOADER             = 'loader'    ;
  LXa_SIDEEFFECT         = 'sideeffect';
  LXa_SAVER              = 'saver'     ;
  LXa_IOSERVICE          = 'ioservice' ;

  LXfBLKS_FORCE          = 1 shl 0;
  LXfBLKS_RAW            = 1 shl 1;

  LXfREADSTRING_FORCE    = $01;
  LXfREADSTRING_RAW      = $02;
  LXfREADSTRING_PARTIAL  = $04;

  LXfBLKW_LEAF           = 1 shl 0;
  LXfBLKW_PARAM          = 1 shl 1;

  LXsLOD_CLASSLIST       = 'loader.classList' ;
  LXsLOD_DOSPATTERN      = 'loader.dosPattern';
  LXsLOD_MACPATTERN      = 'loader.macPattern';

  LXsSAV_OUTCLASS        = 'saver.outClass'   ;
  LXsSAV_DOSTYPE         = 'saver.dosType'    ;
  LXsSAV_MACTYPE         = 'saver.macType'    ;
  LXsSAV_OVERWRITE       = 'saver.overwrite'  ;

  LXu_BLOCKREAD          = '{36A00A47-2664-49E0-BAFF-263E638532B5}';
  LXu_BLOCKWRITE         = '{ADE569E1-E9A1-4AD6-B625-634093660965}';
  LXu_LOADER             = '{73333A41-3C0A-4B0E-A7C5-D76609827A2D}';
  LXu_SIDEEFFECT         = '{3414D56B-31DE-47C7-B751-092B51591DD2}';
  LXu_SAVER              = '{75AD8F36-5B69-413b-A77B-5A78D39AEF51}';
  LXu_MONITOR            = '{2B514D4C-5142-4687-BCEF-B0FD44A33146}';
  LXu_STREAMIO           = '{2884D6EE-4BA7-46D6-A776-8EE22C9FD414}';
  LXu_IOSERVICE          = '{9970CCAD-7322-4CD6-A6BA-963A63DE57D7}';

type
  PLXtBlockHeader = ^LXtBlockHeader;
  LXtBlockHeader = record
    ID    : LXtID4;
    Token : PChar ;
  end;

  PLXtLoadAccess = ^LXtLoadAccess;
  LXtLoadAccess = record
    Filename : PChar   ;
    Types    : ^PGuid  ;
    Monitor  : IUnknown;
    Found    : PGuid   ;
    Opaque   : Integer ;
    Target   : Pointer ;
    Format   : PChar   ;
    Options  : Integer ;
  end;

  ILxBlockRead = interface
  [LXu_BLOCKREAD]
    function  FindBlock (const Head   : PLXtBlockHeader;
                               Flags  : Integer        ;
                               BlkID  : PLXtID4        ) : LxResult;
    procedure &End;
    function  Depth                                      : Integer ;
    function  ReadI1    (      Data   : PChar          ;
                               Count  : Integer        ;
                               OCount : PInteger       ) : LxResult;
    function  ReadI2    (      Data   : PSmallInt      ;
                               Count  : Integer        ;
                               OCount : PInteger       ) : LxResult;
    function  ReadI4    (      Data   : PInteger       ;
                               Count  : Integer        ;
                               OCount : PInteger       ) : LxResult;
    function  ReadU1    (      Data   : PByte          ;
                               Count  : Integer        ;
                               OCount : PInteger       ) : LxResult;
    function  ReadU2    (      Data   : PWord          ;
                               Count  : Integer        ;
                               OCount : PInteger       ) : LxResult;
    function  ReadU4    (      Data   : PUInt32        ;
                               Count  : Integer        ;
                               OCount : PInteger       ) : LxResult;
    function  ReadFP    (      Data   : PSingle        ;
                               Count  : Integer        ;
                               OCount : PInteger       ) : LxResult;
    function  ReadFP8   (      Data   : PDouble        ;
                               Count  : Integer        ;
                               OCount : PInteger       ) : LxResult;
    function  ReadString(      Buf    : PChar          ;
                               Max    : Integer        ;
                               Flags  : Integer        ;
                               OCount : PInteger       ) : LxResult;
    function  ReadIDCode(const List   : PLXtBlockHeader;
                               IDCode : PLXtID4        ) : LxResult;
    function  ReadVX    (      Data   : PDword         ;
                               Count  : Integer        ;
                               OCount : PInteger       ) : LxResult;
  end;

  ILxBlockWrite = interface
  [LXu_BLOCKWRITE]
    function  WriteBlock (const Head  : PLXtBlockHeader;
                                Flags : Integer        ) : LxResult;
    procedure &End;
    function  Depth                                      : Integer ;
    function  WriteI1    (const Data  : PChar          ;
                                Count : Integer        ) : LxResult;
    function  WriteI2    (const Data  : PSmallInt      ;
                                Count : Integer        ) : LxResult;
    function  WriteI4    (const Data  : PInteger       ;
                                Count : Integer        ) : LxResult;
    function  WriteU1    (const Data  : PByte          ;
                                Count : Integer        ) : LxResult;
    function  WriteU2    (const Data  : PWord          ;
                                Count : Integer        ) : LxResult;
    function  WriteU4    (const Data  : PUInt32        ;
                                Count : Integer        ) : LxResult;
    function  WriteFP    (const Data  : PSingle        ;
                                Count : Integer        ) : LxResult;
    function  WriteFP8   (const Data  : PDouble        ;
                                Count : Integer        ) : LxResult;
    function  WriteString(const Str   : PChar          ) : LxResult;
    function  WriteIDCode(const Ident : PLXtBlockHeader) : LxResult;
    function  WriteVX    (      Data  : PDword         ;
                                Count : Integer        ) : LxResult;
  end;

  ILxLoader = interface
  [LXu_LOADER]
    function Recognize   (    Load : PLXtLoadAccess) : LxResult;
    function LoadInstance(    Load : PLXtLoadAccess;
                          out VObj                 ) : LxResult;
    function LoadObject  (    Load : PLXtLoadAccess;
                              Dest : IUnknown      ) : LxResult;
    function Cleanup     (    Load : PLXtLoadAccess) : LxResult;
    function SpawnOptions(out VObj                 ) : LxResult;
  end;

  ILxSaver = interface
  [LXu_SAVER]
    function Verify(      Source:   IUnknown;
                          Message:  IUnknown) : LxResult;
    function Save  (      Source:   IUnknown;
                    const Filename: PChar   ;
                          Monitor:  IUnknown) : LxResult;
  end;

  ILxMonitor = interface
  [LXu_MONITOR]
    function Initialize(Count : UInt32) : LxResult;
    function Increment (Count : UInt32) : LxResult;
  end;

  ILxStreamIO = interface
  [LXu_STREAMIO]
    function Write(Stream : IUnknown) : LxResult;
    function Read (Stream : IUnknown) : LxResult;
  end;

  ILxIOService = interface
  [LXu_IOSERVICE]
    function ScriptQuery(out VObj              ) : LxResult;
    function SetOptions (    Options : IUnknown) : LxResult;
    function PeekOptions                         : IUnknown;
  end;


  { TLxSaver }

  TLxSaver = class(TLxServer, ILxSaver)
    class
    function  GetServerType               : TGuid   ; override;

    function  Verify
              (      Source   : IUnknown;
                     Message  : IUnknown) : LxResult; virtual ;
    function  Save
              (      Source   : IUnknown;
               const Filename : PChar   ;
                     Monitor  : IUnknown) : LxResult; virtual ;
  end;

function LXe_IO_ERROR         : LxResult;
function LXe_REF_BADID        : LxResult;
function LXe_REF_BADPTR       : LxResult;
function LXe_REF_DUPLICATE    : LxResult;
function LXe_IO_SHORTREAD     : LxResult;
function LXe_IO_NEEDGL        : LxResult;
function LXe_IO_TRUNCATED     : LxResult;
function LXe_IO_PARTIALSTRING : LxResult;

implementation

class
function TLxSaver.GetServerType : TGuid;
begin
  Result := ILxSaver;
end;

function TLxSaver.Verify(Source  : IUnknown;
                         Message : IUnknown) : LxResult;
begin
  Result := LXe_NOTIMPL;
end;

function TLxSaver.Save(      Source   : IUnknown;
                       const Filename : PChar   ;
                             Monitor  : IUnknown) : LxResult;
begin
  Result := LXe_NOTIMPL;
end;

function LXe_IO_ERROR         : LxResult; inline;
begin
  Result := LXxFAILCODE(LXeSYS_IO,  1);
end;
function LXe_REF_BADID        : LxResult; inline;
begin
  Result := LXxFAILCODE(LXeSYS_IO, 22);
end;
function LXe_REF_BADPTR       : LxResult; inline;
begin
  Result := LXxFAILCODE(LXeSYS_IO, 23);
end;
function LXe_REF_DUPLICATE    : LxResult; inline;
begin
  Result := LXxFAILCODE(LXeSYS_IO, 24);
end;
function LXe_IO_SHORTREAD     : LxResult; inline;
begin
  Result := LXxFAILCODE(LXeSYS_IO, 25);
end;
function LXe_IO_NEEDGL        : LxResult; inline;
begin
  Result := LXxFAILCODE(LXeSYS_IO, 26);
end;
function LXe_IO_TRUNCATED     : LxResult; inline;
begin
  Result := LXxGOODCODE(LXeSYS_IO, 27);
end;
function LXe_IO_PARTIALSTRING : LxResult; inline;
begin
  Result := LXxGOODCODE(LXeSYS_IO, 28);
end;

end.


